package main

import (
	"context"
	"fmt"
	"github.com/sashabaranov/go-openai"
	"log"
)

var AddToolDefine = openai.Tool{
	Type: "function",
	Function: &openai.FunctionDefinition{
		Name: "AddTool",
		// 不仅写清楚了 AddTool 工具的作用，还举了一个例子，这样可以让大模型更好地理解
		Description: `
        Use this tool for addition calculations.
            example:
                1+2 =?
            then Action Input is: 1,2
        `,
		// 使用标准的 json schema 方式编写了参数名称、类型等，这样也有助于大模型准确理解
		Parameters: `{"type":"object","properties":{"numbers":{"type":"array","items":{"type":"integer"}}}}`,
	},
}

func ToolsChat(message []openai.ChatCompletionMessage, tools []openai.Tool) openai.ChatCompletionMessage {
	c := NewOpenAiClient()
	rsp, err := c.CreateChatCompletion(context.TODO(), openai.ChatCompletionRequest{
		Model:      "qwen-turbo",
		Messages:   message,
		Tools:      tools,
		ToolChoice: "auto",
	})
	if err != nil {
		log.Println(err)
		return openai.ChatCompletionMessage{}
	}

	return rsp.Choices[0].Message
}

func main() {
	toolsList := make([]openai.Tool, 0)
	toolsList = append(toolsList, AddToolDefine)

	prompt := "1+2=? Just give me a number result"
	MessageStore.AddFor(RoleUser, prompt)

	response := ToolsChat(MessageStore.ToMessage(), toolsList)
	toolCall := response.ToolCalls

	fmt.Println("大模型的回复是: ", response.Content)
	fmt.Println("大模型选择的工具是: ", toolCall)
}
